<template>
  <div class="in" ref="bigbox">
    <div class="tit">
      <div class="ul1">
        <div
          v-for="(item, index) in list"
          :key="index"
          :class="{ sta: ind == index }"
          @click="stab(index)"
        >
          {{ item }}
        </div>
      </div>
      <div class="ul2" @click="delxuqiu">
        <img class="img" src="@/assets/img/del.png" alt="" /> 删除需求
      </div>
    </div>
    <div class="list">
      <div class="item" v-for="(item, index) in list1" :key="index">
        <div
          class="le"
          @click="to({ id: item.needId || item.id, stype: item.stype })"
          style="cursor: pointer;"
        >
          <div class="a">{{ item.title }}</div>
          <div class="b">{{ item.intro }}</div>
          <div class="c">
            <span
              >发布时间：{{ item.caAt ? item.caAt.substring(0, 10) : "" }}</span
            >
            <span>地区：{{ item.city }}</span>
            <span
              >状态：<span class="color">已有{{ item.count || 0 }}家领取</span>
            </span>
            <span @click.stop="openDia(item.needId)"
              ><i class="el-icon-warning-outline"></i>举报</span
            >
          </div>
        </div>
        <div class="rig">
          <el-checkbox
            class="rig1"
            v-model="item.sty"
            @change="handChange(index)"
            >选择</el-checkbox
          >
          <el-link
            type="info"
            v-if="item.stype == 0"
            :underline="false"
            style="cursor:default;margin-top:20px"
            >未完成</el-link
          >
          <el-link
            type="primary"
            v-if="item.stype == 1"
            :underline="false"
            style="cursor:default;margin-top:20px;"
            >已完成</el-link
          >
        </div>
      </div>
      <div class="wu" v-if="list1.length == 0">暂无数据</div>
    </div>
    <div class="xuan">
      <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        :current-page.sync="pageNum"
        :page-size="pageSize"
        @current-change="handleCurrentChange1"
        prev-text="上一页"
        next-text="下一页"
        class="aut"
      >
      </el-pagination>
      <el-checkbox class="a" v-model="checkAll" @change="allChange"
        >全选</el-checkbox
      >
    </div>
    <el-dialog title="举报信息" :visible.sync="dialogVisible">
      <el-form :model="form" label-width="100px">
        <el-form-item label="姓名">
          <el-input
            v-model="form.name"
            placeholder="请输入你的姓名"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input
            v-model="form.phone"
            placeholder="请输入你的手机号"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="内容">
          <el-input
            v-model="form.content"
            placeholder="请输入你要举报的内容"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="jubao">确 定</el-button>
      </div>
    </el-dialog>
  </div>
  <!-- </div> -->
</template>

<script>
export default {
  data() {
    return {
      ind: 0,
      list: ["已领取", "我的发布"],
      list1: [],
      ind2: false,
      checkAll: false, //全选
      pageNum: 1,
      pageSize: 10,
      total: 0,
      dialogVisible: false,
      form: {
        name: "",
        phone: "",
        content: "",
        status: 1
      },
      xuqiuId: "" // 举报的需求id
    };
  },
  //注册组件
  components: {},
  //生命周期 - 创建完成（访问当前this实例）
  created() {
    this.getlist();
  },
  //生命周期 - 挂载完成（访问DOM元素）
  mounted() {},
  //计算属性
  computed: {},
  //方法
  methods: {
    getlist() {
      this.$postRequest("/api/mallNeed/myNeedList", {
        userId: localStorage.getItem("userId"),
        status: this.ind,
        pageNum: this.pageNum,
        pageSize: this.pageSize
      }).then(res => {
        this.list1 = res.data.list;
        console.log(this.list1);
        this.total = res.data.total;
      });
    },
    stab(i) {
      if (this.ind != i) {
        this.ind = i;
        this.pageNum = 1;
        this.total = 0;
        this.list1 = [];
        this.getlist();
      }
    },
    handleCurrentChange1() {
      this.getlist();
      window.scrollTo(0, 0);
    },
    to(id) {
      console.log(id);
      this.$router.push({
        name: "releaseitem",
        query: { id: id.id, stype: id.stype }
      });
    },
    delxuqiu() {
      var arr = [];
      for (var i = 0; i < this.list1.length; i++) {
        if (this.list1[i].sty) {
          arr.push(this.list1[i]);
        }
      }
      if (arr.length == 0) {
        this.$message.error("请选择要删除的需求");
      } else {
        this.$confirm("你确定要删除吗", "删除", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          var num = 0;
          for (var m = 0; m < arr.length; m++) {
            this.$postRequest("/api/mallNeed/deleteMyNeed", {
              userId: localStorage.getItem("userId"),
              status: this.ind,
              needId: arr[m].myNeedId || arr[m].id
            }).then(res => {
              num++;
              if (num == arr.length) {
                this.$message.success("删除成功");
                this.pageNum = 1;
                this.total = 0;
                this.list1 = [];
                this.getlist();
              }
            });
          }
        });
      }
    },
    allChange(a) {
      console.log(a);
      this.list1 = this.list1.map(item => {
        item.sty = a;
        return item;
      });
    },
    handChange(a) {
      this.checkAll = this.list1.every(item => {
        return item.sty;
      });
      this.$forceUpdate();
    },
    openDia(id) {
      this.xuqiuId = id;
      this.dialogVisible = true;
    },
    jubao() {
      var tellreg = /^1[0-9]{10}$/;
      if (!this.form.name) {
        this.$message.error("请输入你的姓名");
      } else if (!this.form.phone) {
        this.$message.error("请输入你的手机号");
      } else if (!this.form.content) {
        this.$message.error("请输入你要举报的内容");
      } else if (!tellreg.test(this.form.phone)) {
        this.$message({
          message: "手机号错误，请检查后重新填写",
          type: "warning"
        });
      } else {
        this.form.type = "1";
        this.form.backId = this.xuqiuId;
        this.$postRequest("/api/trotocol/saveFeedback", this.form).then(res => {
          this.$message.success("举报成功");
          this.dialogVisible = false;
        });
      }
    }
  }
};
</script>
<style scoped lang='scss'>
.in {
  width: 100%;
  overflow-y: auto;
  background: #ffffff;
  border: 1px solid #eeeeee;
  .tit {
    width: 100%;
    height: 50px;
    background: #ffffff;
    border-bottom: 1px solid #eeeeee;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .ul1 {
      display: flex;
      height: 100%;
      align-items: center;
      div {
        width: 100px;
        height: 50px;
        line-height: 50px;
        text-align: center;
        font-size: 16px;
        font-family: PingFang SC;
        font-weight: 500;
        color: #000000;
        cursor: pointer;
        &.sta {
          color: #1a69e0;
          position: relative;
          &::after {
            content: "";
            width: 100px;
            height: 2px;
            background: #1a69e0;
            position: absolute;
            bottom: 0;
            left: 0;
          }
        }
      }
    }
    .ul2 {
      padding-right: 20px;
      cursor: pointer;
      color: #1a69e0;
      display: flex;
      align-items: center;
      .img {
        width: 20px;
        height: 20px;
        margin-right: 8px;
      }
      &:hover {
        opacity: 0.8;
      }
    }
  }
  .list {
    width: 100%;
    background: #fff;
    padding: 0 20px 10px;
    .item {
      display: flex;
      justify-content: space-between;
      align-items: center;
      border-bottom: 1px solid #eeeeee;
      .le {
        width: 860px;
        padding: 20px 0;
        .a {
          font-size: 16px;
          font-family: Microsoft YaHei;
          font-weight: 400;
          color: #000000;
          margin-bottom: 16px;
        }
        .b {
          font-size: 14px;
          font-family: PingFang SC;
          font-weight: 500;
          color: #666666;
          line-height: 20px;
          margin-bottom: 20px;
        }
        .c {
          height: 14px;
          font-size: 14px;
          font-family: PingFang SC;
          font-weight: 500;
          color: #999999;
          & > span {
            margin-right: 30px;
            .color {
              color: #e0441a;
            }
          }
        }
      }
      .rig {
        width: 120px;
        text-align: center;
        display: flex;
        flex-direction: column;
        align-items: center;
        .d {
          width: 120px;
          height: 40px;
          margin-bottom: 18px;
          font-size: 16px;
          font-family: Microsoft YaHei;
          font-weight: 400;
          text-align: center;
          line-height: 40px;
        }
        .a {
          background: #eeeeee;
          color: #000000;
        }
        .b {
          background: #1a69e0;
          color: #ffffff;
        }
        .c {
          font-size: 14px;
          font-family: PingFang SC;
          font-weight: 500;
          color: #666666;
        }
      }
    }
    .aut {
      text-align: center;
      margin-top: 14px;
      margin-bottom: 14px;
    }
    /deep/ .el-pagination button,
    .el-pagination span:not([class*="suffix"]) {
      width: 60px;
    }
    .wu {
      width: 100%;
      height: 100px;
      line-height: 100px;
      text-align: center;
    }
  }
  .xuan {
    display: flex;
    align-items: center;
    justify-content: center;

    .a {
      margin-right: 20px;
    }
  }
  .aut {
    flex: 1;
    text-align: center;
    margin-top: 14px;
    margin-bottom: 14px;
  }
  /deep/ .el-pagination button,
  .el-pagination span:not([class*="suffix"]) {
    width: 60px;
  }
}
</style>